package com.tywj.buscustomerapp.utils;

import android.text.format.Time;
import android.util.Log;

import com.tywj.buscustomerapp.model.bean.DateJoe;
import com.tywj.buscustomerapp.model.bean.DateJoe1;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * @Describe: TODO  时间工具类
 * * * *
 * @Date: 2014/04/10 17:54
 * @version: 1.0
 */
public class TimeRender {

    public static List<DateJoe> getThisWeekDate(Date date){
        List<DateJoe> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd"); // 设置时间格式
        SimpleDateFormat sdf1 = new SimpleDateFormat("dd"); // 设置时间格式
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (0 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.SUNDAY);
        int day = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        for (int i = 0; i < 21; i++) {
            DateJoe dateJoe = new DateJoe();
            dateJoe.setWeek(i%7);
            dateJoe.setDay(sdf1.format(cal.getTime()));
            dateJoe.setTotalDate(sdf.format(cal.getTime()));
            if (!compareTime3(dateJoe.getTotalDate())) {
                dateJoe.setWeek(0);
                dateJoe.isShow = false;
            }
            list.add(dateJoe);
            cal.add(Calendar.DATE,1);
        }
        return list;
    }


    public static List<DateJoe> getThisWeekDate1(Date date){
        List<DateJoe> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd"); // 设置时间格式
        SimpleDateFormat sdf1 = new SimpleDateFormat("dd"); // 设置时间格式
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (0 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.SUNDAY);
        int day = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        for (int i = 0; i < 21; i++) {
            DateJoe dateJoe = new DateJoe();
            if (i%7==0 || i%7 == 6){
                dateJoe.setWeek(true);
            }
            dateJoe.setDay(sdf1.format(cal.getTime()));
            dateJoe.setTotalDate(sdf.format(cal.getTime()));
            if (!compareTime3(dateJoe.getTotalDate())) {
                dateJoe.setWeek(true);
                dateJoe.isShow = false;
            }
            list.add(dateJoe);
            cal.add(Calendar.DATE,1);
        }
        return list;
    }

    /**
     * 获取当前日期是星期几<br>
     *
     * @param date
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(Date date) {
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
        String currSun = dateFm.format(date);
        return weekDays[w]+"_"+currSun;
    }


    // 将字符串转为时间戳

    public static String getTime(String user_time) {
        String re_time = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date d;
        try {
            d = sdf.parse(user_time);
            long l = d.getTime();
            String str = String.valueOf(l);
            re_time = str.substring(0, 10);

        } catch (ParseException e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        }
        return re_time;
    }


    public static String getTime1(String user_time) {
        String re_time = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d;
        try {

            d = sdf.parse(user_time);
            long l = d.getTime();
            String str = String.valueOf(l);
            re_time = str.substring(0, 10);

        } catch (ParseException e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        }
        return re_time;
    }

    public static String getTime2(String user_time) {
        String re_time = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date d;
        try {

            d = sdf.parse(user_time);
            long l = d.getTime();
            String str = String.valueOf(l);
            re_time = str.substring(0, 10);

        } catch (ParseException e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        }
        return re_time;
    }

    public static long getTime4(String user_time) {
        SimpleDateFormat dateFormate = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        Date d = null;
        try {
            d = dateFormate.parse(user_time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        long l = d.getTime();

        return l;
    }
    public static String getFullDateStr5() {
        Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料
        t.setToNow(); // 取得系统时间。
        int year = t.year;
        int month = t.month+1;
        int date = t.monthDay;
        int hour = t.hour+1;
        int min = t.minute;
        int s = t.second;
        String m = month+"";
        String d = date+"";
        if (month<10){
            m = "0"+month;
        }
        if (date<10){
            d = "0"+date;
        }
        String strDate = year+"."+m+"."+d;
        return strDate;
    }

    public static String[] getFullDateStr9() {
        Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料
        t.setToNow(); // 取得系统时间。
        int year = t.year;
        int month = t.month+1;
        String[] strDate = new String[2];
        strDate[0] = String.valueOf(year);
        strDate[1] = String.valueOf(month);
        return strDate;
    }

    public static String getMonth() {
        Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料
        t.setToNow(); // 取得系统时间。
        int month = t.month+2;
        return String.valueOf(month);
    }

    public static String[] getFullDateStr10() {
        Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料
        t.setToNow(); // 取得系统时间。
        int year = t.year;
        int month = t.month+1;
        String[] strDate = new String[2];
        strDate[0] = String.valueOf(year);
        strDate[1] = String.valueOf(month);
        return strDate;
    }

    public static String getFullDateStr8() {
        Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料
        t.setToNow(); // 取得系统时间。
        int hour = t.hour;
        int min = t.minute;
        int s = t.second;
        String strDate = hour+":"+min+":"+s;
        return strDate;
    }

    public static int getFullDateStr6() {
        Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料
        t.setToNow(); // 取得系统时间。
        int year = t.year;
        int month = t.month+1;
        int date = t.monthDay;
        return date;
    }

    public static int getFullDateStr7() {
        Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料
        t.setToNow(); // 取得系统时间。
        int year = t.year;
        int month = t.month+1;
        int date = t.monthDay;
        return month;
    }
    public static String timeToStr(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  // hh:mm:ss
        String date = sdf.format(new Date(time* 1000));
        return date;
    }
    public static String getTime3(String user_time) {
        String re_time = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        Date d;
        try {

            d = sdf.parse(user_time);
            long l = d.getTime();
            String str = String.valueOf(l);
            re_time = str.substring(0, 10);

        } catch (ParseException e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        }
        return re_time;
    }

    // 将时间戳转为字符串
    public static String getStrTime(String cc_time) {
        String re_StrTime = null;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
// 例如：cc_time=1291778220
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));

        return re_StrTime;
    }
    // 将时间戳转为字符串
    public static String getStrTime4(String cc_time) {
        String re_StrTime = null;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
// 例如：cc_time=1291778220
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));

        return re_StrTime;
    }
    // 将时间戳转为字符串
    public static String getStrTime3(String cc_time) {
        String re_StrTime = null;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
// 例如：cc_time=1291778220
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));

        return re_StrTime;
    }

    public static String getStrTime2(String cc_time) {
        String re_StrTime = null;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
// 例如：cc_time=1291778220
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));

        return re_StrTime;
    }

    // 将时间戳转为字符串
    public static String getStrTime1(String cc_time) {
        String re_StrTime = null;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 例如：cc_time=1291778220
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));

        return re_StrTime;
    }

    public static String getFullDateString() {
        String strDate = "";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");
        Date curDate = new Date(System.currentTimeMillis());//
        strDate = formatter.format(curDate);

        return strDate;
    }

    public static String getFullDateStr() {
        String strDate = "";
        Calendar c = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");    // HH:mm:ss
//        Date curDate = new Date(System.currentTimeMillis());//
        formatter.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
        strDate = formatter.format(c.getTime());
        return strDate;
    }
    public static String getPointDate(int distanceDay) {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy.MM.dd");
        Date beginDate = new Date();
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) + (distanceDay-1));
        Date endDate = null;
        try {
            endDate = dft.parse(dft.format(date.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dft.format(endDate);
    }

    public static String getFullDateStr3() {
//        Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料
//        t.setToNow(); // 取得系统时间。
//        int year = t.year;
//        int month = t.month+1;
//        int day  = t.monthDay;
//        String strDate = year+"."+month+"."+day;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");    // HH:mm:ss
        Date curDate = new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000);//
        String strDate = formatter.format(curDate);
        return strDate;
    }

    public static String getFullDateStr1() {
        String strDate = "";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");    // HH:mm:ss
        Date curDate = new Date(System.currentTimeMillis());//
        strDate = formatter.format(curDate);

        return strDate;
    }

    public static String getFullDateStr2() {
        String strDate = "";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");    // HH:mm:ss
        Date curDate = new Date(System.currentTimeMillis() + 2 * 60 * 60 * 1000+ 15*60*1000);//
        strDate = formatter.format(curDate);
        String m1 = strDate.substring(0, strDate.lastIndexOf(":") + 1);
        String m = strDate.substring(strDate.lastIndexOf(":") + 1, strDate.length());
        String h = strDate.substring(strDate.lastIndexOf(" ") + 1, strDate.lastIndexOf(":"));
        String h1 = strDate.substring(0, strDate.lastIndexOf(" ") + 1);
        int mi = Integer.valueOf(m);
        if (mi < 5 && mi >= 0) {
            strDate = m1 + "00";
        } else if (mi <= 15 && mi >= 5) {
            strDate = m1 + "10";
        } else if (mi <= 25 && mi > 15) {
            strDate = m1 + "20";
        } else if (mi <= 35 && mi > 25) {
            strDate = m1 + "30";
        } else if (mi <= 45 && mi > 35) {
            strDate = m1 + "40";
        } else if (mi <= 55 && mi > 45) {
            strDate = m1 + "50";
        } else if (mi <= 60 && mi > 55) {
            //strDate = m1 + "00";
            int hi = Integer.valueOf(h) + 1;
            String tmpStr = String.valueOf(hi);
            if (hi < 10) {
                tmpStr = "0" + tmpStr;
            }
            strDate = h1 + tmpStr + ":00";
        }
        return strDate;
    }

    public static long getSetTime(String begin) {
        long hours = 0;
        long min = 0;
        SimpleDateFormat dateFormate = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm");
        try {
            Date date1 = dateFormate.parse(begin);
            long diff = date1.getTime();
            return diff;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * @param begin 开始时间
     * @param end   当前时间
     *              <p/>
     *              时间格式
     * @return 两个时间点之间的差，单位小时
     */
    public static long compareTime(String begin, String end) {
        long hours = 0;
        long min = 0;
        SimpleDateFormat dateFormate = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm");
        try {
            Date date1 = dateFormate.parse(begin);
            Date date2 = dateFormate.parse(end);
            long diff = date2.getTime() - date1.getTime();
            min = diff / (1000 * 60); // 分钟
            hours = diff / (1000 * 60 * 60);// 小时
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return hours;  // + "h" + min + "'"
    }

    public static long compareTime1(String begin, String end) {
        long hours = 0;
        long min = 0;
        SimpleDateFormat dateFormate = new SimpleDateFormat(
                "yyyy-MM-dd");
        try {
            Date date1 = dateFormate.parse(begin);
            Date date2 = dateFormate.parse(end);
            long diff = date2.getTime() - date1.getTime();
            min = diff / (1000 * 60); // 分钟
            hours = diff / (1000 * 60 * 60);// 小时
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return hours;  // + "h" + min + "'"
    }

    public static boolean compareTime2(String begin, String end) {
        SimpleDateFormat dateFormate = new SimpleDateFormat(
                "yyyy.MM.dd");
        try {
            Date date1 = dateFormate.parse(begin);
            Date date2 = dateFormate.parse(end);
            long diff = date2.getTime() - date1.getTime();
            return diff>=0;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return true;
          // + "h" + min + "'"
    }

    public static int isOldDay(String begin) {
        long min = 0;
        SimpleDateFormat dateFormate = new SimpleDateFormat(
                "yyyy.MM.dd");
        try {
            Date date1 = dateFormate.parse(begin);
            Date date2 = new Date();
            long diff = date2.getTime() - date1.getTime();
            if (diff>0){
                if ((diff/1000/60/60)<24){
                    return 1;
                }else{
                    return 2;
                }
            }else{
                return 0;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 2;
    }


    public static boolean compareTime6(String begin) {
        SimpleDateFormat dateFormate = new SimpleDateFormat(
                "HH:mm");
        long min = 0;
        try {
            Date date1 = dateFormate.parse(begin);
            Date date2 = new Date();
            long diff = date2.getTime() - date1.getTime();
            min = diff / (1000 * 60); // 分钟
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return min<6;  // + "h" + min + "'"
    }

    public static boolean compareTime666(String begin) {
        SimpleDateFormat dateFormate = new SimpleDateFormat(
                "yyyy.MM.dd HH:mm");
        long min = 0;
        try {
            Date date1 = dateFormate.parse(begin);
            Date date2 = new Date();
            long diff = date1.getTime() - date2.getTime();
            min = diff / (1000 * 60); // 分钟
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return min<=0;  // + "h" + min + "'"
    }

    public static boolean compareTime66(String begin) {
        SimpleDateFormat dateFormate = new SimpleDateFormat(
                "yyyy.MM.dd HH:mm");
        long min = 0;
        try {
            Date date1 = dateFormate.parse(begin);
            Date date2 = new Date();
            long diff = date2.getTime() - date1.getTime();
            min = diff / (1000 * 60); // 分钟
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return min<=0;  // + "h" + min + "'"
    }

    public static boolean compareTime3(String begin) {
        long hours = 0;
        long min = 0;
        SimpleDateFormat dateFormate = new SimpleDateFormat(
                "yyyy.MM.dd");
        try {
            Date date1 = dateFormate.parse(begin);
            Date date2 = dateFormate.parse(getFullDateString());
            long diff = date1.getTime() - date2.getTime();
            System.out.println("success!!!!!!!!!!!"+min);
            min = diff / (1000 * 60); // 分钟
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("false!!!!!!!!!!!");
        }
        return min>=0;
    }
}
